//
//  LoginViewController.m
//  DS_PJ_01
//
//  Created by 宋畅 on 2016/12/15.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "LoginViewController.h"

#import "UserInfoModel.h"

@interface LoginViewController ()

//账号
@property(nonatomic,strong)UITextField *accountTextField;

//密码
@property(nonatomic,strong)UITextField *passwordTextField;

//自动登录按钮
@property(nonatomic,strong)UIButton *autoLoginButton;

//归属
@property(nonatomic,strong)UIButton *loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadUI];
    
}


-(void)loadUI
{
    self.accountTextField.frame = CGRectMake(5, sScreenHeight/2 - 70, sScreenWith-10, 45);

    self.passwordTextField.frame = CGRectMake(_accountTextField.left,
                                              _accountTextField.bottom ,
                                              _accountTextField.width,
                                              _accountTextField.height);
    
    self.loginButton.frame = CGRectMake(_passwordTextField.left,
                                        _passwordTextField.bottom+5,
                                        _passwordTextField.width,
                                        _passwordTextField.height);

    self.autoLoginButton.frame = CGRectMake(sScreenWith - 120,_loginButton.bottom + 20, 100, 40);
    
    UILabel *companyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, sScreenHeight - 70, sScreenWith, 70)];
    companyLabel.font = BOLD_FONT(14);
    companyLabel.textColor = RGB(50, 50, 50, 1);
    companyLabel.text = @"BY CHIEF INFORMATION TECHNOLOGY";
    companyLabel.textAlignment = NSTextAlignmentCenter;
    companyLabel.numberOfLines = 2;
    companyLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self.view addSubview:companyLabel];
    
}


#pragma mark ----------- 懒加载 --------------
-(UITextField *)accountTextField
{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]init];
        _accountTextField.textColor = RGB(150, 150, 150, 1);
        _accountTextField.placeholder = NSLocalizedString(@"Please Input Phone Number", @"账号");
        [_accountTextField setCornerRadius:5 andColor:[UIColor blackColor] andWidth:1.5];
        
        [self.view addSubview:_accountTextField];
    }
    
    return _accountTextField;
}


-(UITextField *)passwordTextField
{
    if (!_passwordTextField) {
        _passwordTextField = [[UITextField alloc]init];
        _passwordTextField.textColor = RGB(150, 150, 150, 1);
        _passwordTextField.placeholder = NSLocalizedString(@"Please Input Password", @"密码");
        [_passwordTextField setCornerRadius:5 andColor:[UIColor blackColor] andWidth:1.5];
        
        [self.view addSubview:_passwordTextField];
    }
    
    return _passwordTextField;
}



-(UIButton *)loginButton
{
    if (!_loginButton) {
        _loginButton = [UIButton setButton_style_one];
        [_loginButton setTitle:NSLocalizedString(@"Login", @"登录") forState:0];
        [_loginButton setCornerRadius:5 andColor:[UIColor clearColor] andWidth:1.5];
        
        [_loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_loginButton];
    }
    
    return _loginButton;
}


-(UIButton *)autoLoginButton
{
    if (!_autoLoginButton) {
        _autoLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _autoLoginButton.backgroundColor = [UIColor clearColor];
        _autoLoginButton.selected = NO;
        [_autoLoginButton addTarget:self action:@selector(isAutoLoginAction:) forControlEvents:UIControlEventTouchUpInside];

        
        [_autoLoginButton setTitle:NSLocalizedString(@"autoLoginName", @"自动登录") forState:0];
        [_autoLoginButton setTitleColor:[UIColor blackColor] forState:0];
        _autoLoginButton.titleLabel.font = FONT(14);
        
        [self.view addSubview:_autoLoginButton];
    }
    
    return _autoLoginButton;
}




#pragma mark --------- action -------------
/*
 点击登录触发，这里是访问后台
 */
-(void)loginAction
{
    [DSDownload login_phone:@"12345678"
                   password:@"88888888"
                    success:^(id responseObject, int code, NSString *message) {
                        
                        if (code == 1) {
                            
                            
                            NSString *userInfoStr = [NSString dataToJson:responseObject[@"data"]];
                            
                            if ([userInfoStr saveValueToUserDefalutsWithKey:@"userInfo"]) {
                                
                           	     [DSNoticeManager postNotification:LOGIN_SUCCESS object:nil];
                                
                            } else {
                                
                                [NSObject showAlertMessage:@"登录成功但是数据出错"];
                                
                            }
                            
//                            NSMutableArray  *dataArray = [[NSMutableArray alloc]init];
//                            for (NSDictionary *dic in responseObject[@"data"]) {
//                                UserInfoModel *model = [[UserInfoModel alloc]initWithDictionary:dic];
//                                [dataArray addObject:model];
//                            }
//                            self.dataArray = dataArray;
                            
                        }
                        
                        [NSObject showAlertMessage:@"success"];
                        
                    } failure:^(NSError *error) {
                        
                        [NSObject showAlertMessage:@"error"];
                        
                    }];
}

/*
 button.selected ----  NO为不设置为自动登录，YES为设置为自动登录
 */
-(void)isAutoLoginAction:(UIButton *)button
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
